package com.example.gulimall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.service.ProductAttrValueService;
import com.example.gulimall.product.vo.AttrGroupWithAttersVo;
import com.example.gulimall.product.vo.item.SkuItemVo;
import com.example.gulimall.product.vo.item.SpuBaseAttrVo;
import com.example.gulimall.product.vo.item.SpuItemGroupAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.product.dao.AttrGroupDao;
import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired private AttrService  attrService;
    @Autowired private ProductAttrValueService productAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                Wrappers.<AttrGroupEntity>lambdaQuery()
                .eq(catId != 0, AttrGroupEntity::getCatelogId, catId)
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetails(AttrGroupEntity attrGroup) {
        this.baseMapper.updateById(attrGroup);

        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrGroupId(attrGroup.getAttrGroupId());
        entity.setGroupName(attrGroup.getAttrGroupName());
        this.attrAttrgroupRelationService.update(
                entity,
                Wrappers.<AttrAttrgroupRelationEntity>lambdaUpdate()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroup.getAttrGroupId())
        );
    }

    @Override
    public List<AttrGroupWithAttersVo> getAttrGroupWithAtters(Long catelogId) {
        List<AttrGroupEntity> entities = this.baseMapper.selectList(
                Wrappers.<AttrGroupEntity>lambdaQuery()
                        .eq(AttrGroupEntity::getCatelogId, catelogId)
        );

        return entities.stream().map(
                a -> {
                    AttrGroupWithAttersVo group = new AttrGroupWithAttersVo();
                    BeanUtils.copyProperties(a, group);
                    group.setAttrs(this.attrService.getRelationAttr(a.getAttrGroupId()));
                    return group;
                }
        ).collect(Collectors.toList());

    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {

        // 根据分类ID查询出所有的分组
        List<SpuItemGroupAttrVo> list = this.baseMapper.selectGroupByCatalogId(catalogId);

        // 查询SPU对应的属性
        List<SpuBaseAttrVo> attrs = this.attrService.selectBatchByCatalogId(catalogId);
        Set<Long> attrIds = attrs.stream().map(SpuBaseAttrVo::getAttrId).collect(Collectors.toSet());

        // 查询spu对应的属性值
        List<SpuBaseAttrVo> attrVos = this.productAttrValueService.selectBatchByAttrIds (attrIds);
        Map<Long, String> attrValueMap = attrVos.stream()
                .collect(Collectors.toMap(SpuBaseAttrVo::getAttrId, SpuBaseAttrVo::getAttrValue));

        // 合并 属性 与 属性值
        for (SpuBaseAttrVo attr : attrs) {
            attr.setAttrValue(attrValueMap.get(attr.getAttrId()));
        }

        Map<Long, List<SpuBaseAttrVo>> attrGroup = new HashMap<>();
        for (SpuBaseAttrVo attr : attrs) {
            List<SpuBaseAttrVo> vos = attrGroup.get(attr.getGroupId());
            if (CollectionUtil.isEmpty(vos)) {
                vos = new ArrayList<>();
                vos.add(attr);
                attrGroup.put(attr.getGroupId(), vos);
            }
            vos.add(attr);
            attrGroup.put(attr.getGroupId(), vos);
        }

        for (SpuItemGroupAttrVo attrVo : list) {
            attrVo.setAttrs(attrGroup.get(attrVo.getGroupId()));
        }

        return list;
    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrGroupWithAttrsBySpuIdJoin(Long spuId, Long catalogId) {
        return this.baseMapper.selectAttrGroupWithAttrsBySpuIdJoin(spuId, catalogId);
    }

}